<?php
class ome_mdl_order_groupon extends dbeav_model {
	
	var $has_many = array ('order_groupon_items' => 'order_groupon_items' );
	
	function io_title($filter, $ioType = 'csv') {
		switch ($filter) {
			case 'export' :
				$this->oSchema ['csv'] [$filter] = array ('*:订单号' => 'order_bn', '*:收件人' => 'consignee_name', '*:省' => 'consignee_area_province', '*:市' => 'consignee_area_city', '*:区（县）' => 'consignee_area_county', '*:收件人地址' => 'ship_addr', '*:手机' => 'consignee_mobile', '*:电话' => 'consignee_tel', '*:快递公司' => 'shipping_name', '*:发货时间/备注' => 'custom_mark', '*:购买时间' => 'createtime', '*货品明细代号' => 'product_bn', '*:数量' => 'product_nums', '*:单价' => 'product_price' , '*:配送费用' => 'cost_freight', '*:卖家备注' => 'mark_text', '*:货到付款' => 'shipping_cod');
				break;
		
		}
		$this->ioTitle [$ioType] [$filter] = array_keys ( $this->oSchema [$ioType] [$filter] );
		return $this->ioTitle [$ioType] [$filter];
	}
	
	//csv导出
	function fgetlist_csv(&$data, $filter, & $offset, $exportType = 1) {
		$title = array ();
		foreach ( $this->io_title ( 'export' ) as $k => $v ) {
			$title [] = $v;
		}
		$data ['title'] = '"' . implode ( '","', $title ) . '"';
		
		$orders = $this->getGrouponOrders ( $_GET ['order_groupon_id'] );
		$data ['contents'] = array ();
		$shipping_cod = array ('false' => '否', 'true' => '否' );
		foreach ( $orders as $order ) {
			$_ship_addr1 = explode(':', $order['ship_area']);

			$_ship_addr = explode('/', $_ship_addr1[1]);
			$consigner_area = $_ship_addr[0];
			$consigner_area_city = $_ship_addr[1];
			$ship_area = $_ship_addr[2];
			$createtime = date("Y-m-d H:i:s",$order['createtime']);
		    $order ['order_bn'] = "=\"\"".$order ['order_bn']."\"\"";
			$data ['contents'] [] = '"' . $order ['order_bn'] . '","' . $order ['ship_name'] . '","' . $consigner_area . '","' . $consigner_area_city . '","' . $ship_area . '","' . $order ['ship_addr'] . '","' . $order ['ship_mobile'] . '","' . $order ['ship_tel'] . '","' . $order ['123'] . '","' . $order ['custom_mark'] . '","' . $createtime . '","' . $order ['bn'] . '","' . $order ['nums'] . '","' . $order ['price'] . '","' . $order ['cost_freight'] . '","' . $order ['mark_text'] . '","' . $shipping_cod[$order ['is_cod']] . '"';
		}
		
		$offset = 1;
		
		return false;
	}
	
	function getGrouponOrders($order_groupon_id) {
		$list = $this->db->select ( 'select order_id from sdb_ome_order_groupon_items where order_groupon_id=' . $order_groupon_id );
		$order_ids = array ();
		foreach ( $list as $row ) {
			$order_ids [] = $row ['order_id'];
		}
		$list = $this->db->select ( 'select o.order_bn,o.consigner_name,o.ship_area,o.ship_addr,o.ship_mobile,o.ship_tel,o.ship_name,o.custom_mark,o.createtime,it.bn,it.nums,it.price,o.cost_freight,o.mark_text,o.is_cod 
		from sdb_ome_orders as o left JOIN sdb_ome_order_items it on it.order_id = o.order_id 
		where o.order_id in(' . implode ( ',', $order_ids ) . ') order by ship_status' );
		
		return $list;
	}

}
?>